<template>
  <view class="coupon-wrap">
    <!-- <van-tabs color="#2E87FF" :sticky="true" :active="currentActive" bind:change="onChange"> -->
    <van-tabs color="#2E87FF" :active="currentActive" @change="listChange" :z-index="99">
      <van-tab title="打球卡">
        <view class="my-vip">
					<view class="tip" id="header-top">
						温馨提示：打球卡金额仅限用户购买俱乐部活动时使用。
					</view>
          <view
            @click="toDetail(item.clubId)"
            v-for="(item, index) in list"
            :key="index"
            class="vip-item"
          >
            <view class="top">
              <view class="icon">
                <image v-if="item.clubIcon" :src="item.clubIcon" mode>
                <image v-else :src="'@/static/images/image-default.png'|addBaseImgUrl" mode>
              </view>
              <view class="title line-clamp-2">{{item.clubName}}</view>
            </view>
						<view class="bottom flex-box">
							<view class="bottom_left">
								
							</view>
							<view class="bottom_right">
							<view class="bottom_right_cost">
								<view class="label">总余额：</view>
								<view class="price">￥
									<text>{{item.account | floorTwo}}</text>
								</view>
							</view>
							<view v-if="item.clubCardId" class="topUp" @click.stop="topUp(item.clubId)">
								充值
							</view>
            </view>
						</view>
            
          </view>
          <xjx-nodata v-if="list.length==0&&pageLoaded"></xjx-nodata>
          <view v-if="list.length==0&&pageLoaded&&toBuyFlag" class="guide-box flex-box">
            <view @click="toBuyVipCard" class="guide-btn">立即选购</view>
          </view>
        </view>
      </van-tab>
      <van-tab title="优惠券">
        <view class="coupon">
          <template v-if="pageLoad">
            <template v-if="couponList.length">
              <coupon-con  v-for="itemData in couponList" :key="itemData.id" :item="itemData"></coupon-con>
            </template>
            <xjx-nodata v-else></xjx-nodata>
            <view class="scroll-bottom">
              <van-loading v-if="pageFlag"
                          type="spinner"
                          size="24px" />
              <text v-if="couponList.length === allCount && couponList.length">我也是有底线的~</text>
            </view>
          </template>
          <!-- <van-tabs color="#2E87FF" :z-index="1000" :active="couponActive" @change="couponChange">
            <van-tab title="全部">
              <template v-if="pageLoad">
                <coupon-con v-if="couponList.length" :list="couponList"></coupon-con>
                <xjx-nodata v-else></xjx-nodata>
                <view class="scroll-bottom">
                  <van-loading v-if="pageFlag"
                              type="spinner"
                              size="24px" />
                  <text v-if="couponList.length === allCount && couponList.length">我也是有底线的~</text>
                </view>
              </template>
            </van-tab>
            <van-tab title="未使用">
              <template v-if="pageLoad">
                <coupon-con v-if="couponList.length" :list="couponList"></coupon-con>
                <xjx-nodata v-else></xjx-nodata>
                <view class="scroll-bottom">
                  <van-loading v-if="pageFlag"
                              type="spinner"
                              size="24px" />
                  <text v-if="couponList.length === allCount && couponList.length">我也是有底线的~</text>
                </view>
                </template>
            </van-tab>
            <van-tab title="已使用">
              <template v-if="pageLoad">
                <coupon-con v-if="couponList.length" :list="couponList"></coupon-con>
                <xjx-nodata v-else></xjx-nodata>
                <view class="scroll-bottom">
                  <van-loading v-if="pageFlag"
                              type="spinner"
                              size="24px" />
                  <text v-if="couponList.length === allCount && couponList.length">我也是有底线的~</text>
                </view>
              </template>
            </van-tab>
            <van-tab title="已过期">
              <template v-if="pageLoad">
                <coupon-con v-if="couponList.length" :list="couponList"></coupon-con>
                <xjx-nodata v-else></xjx-nodata>
                <view class="scroll-bottom">
                  <van-loading v-if="pageFlag"
                              type="spinner"
                              size="24px" />
                  <text v-if="couponList.length === allCount && couponList.length">我也是有底线的~</text>
                </view>
              </template>
            </van-tab>
          </van-tabs> -->
          <!-- 列表无数据loading -->
          <view class="list-loading"
                v-if="!pageLoad">
            <van-loading size="30px"
                        vertical>加载中...</van-loading>
          </view>
        </view>
      </van-tab>
    </van-tabs>
    <!-- 固定 -->
    <view class="fixed-line" v-if="currentActive ==1">
      <view v-for="(item, index) in navList" :key="item.id" :class="['fixed-nav', checkIndex == index ? 'active' : '']" @click="checkIndex=index;couponChange()">{{item.name}}</view>
    </view>
  </view>
</template>

<script>
import { mapState, mapGetters, mapMutations } from 'vuex';
import couponCon from "./couponCon";
export default {
  data() {
    return {
      // 导航列表
      navList: [{
        id: 1,
        name: '全部'
      },{
        id: 2,
        name: '未使用'
      },{
        id: 3,
        name: '已使用'
      },{
        id: 4,
        name: '已过期'
      },],
      // 当前选中tab
      currentActive: 0,
      // 当前选中状态
      checkIndex: 0,
      list: [],
      pageLoaded: false,
      toBuyFlag: false,
      tabHeight: 0,
      // 全部优惠券
      couponList: [],
      // 分页数据
      pageData: {
        pageNo: 1,
        pageSize: 20
      },
      // 滚动开关
      pageFlag: false,
      // 列表数据总数
      allCount: 0,
      // 是否滑动到底部
      endFlag: false,
      // 页面加载loading
      pageLoad: false,
			btnShow:false//是否展示充值按钮
    };
  },
  components: {
    couponCon: couponCon
  },
  onLoad(data) {
    this.init();
    this.getVipCardList();
    this.queryCouponList(0)
  },
  onShow() {},
  computed: {
    ...mapState(['userInfo', 'accessToken'])
  },
  methods: {
    async init() {
      const param = {
        'ACCESS-TOKEN': this.accessToken
      };
      const res = await this.$api.getMyVipList(param);
      if (res && res.code == 200) {
        this.pageLoaded = true;
        this.list = res.data;
      }
    },
    toDetail(id) {
      uni.navigateTo({
        url: `/personalCenter/myVip/detail/detail?id=${
          id && id != 'undefind' ? id : ''
        }`
      });
    },
    async getVipCardList() {
      const param = {
        'ACCESS-TOKEN': this.accessToken
      };
      const res = await this.$api.getClubVipCardAll(param);
      if (res && res.code == 200) {
        if (res.data.length > 0) {
          this.toBuyFlag = true;
        }
      }
    },
		topUp(id){
			uni.navigateTo({
			  url: `/personalCenter/vipCard/index/index?clubId=${id}`
			});
		},
    toBuyVipCard(id) {
      uni.navigateTo({
        url: '/personalCenter/vipCard/list/list'
      });
    },
    // 滚动底部加载
    onReachBottom() {
      if (this.pageFlag) return
      if (this.couponList.length < this.allCount) {
        this.pageData.pageNo +=1
        this.pageFlag = true
        this.queryCouponList(this.checkIndex)
      }else {
        this.endFlag = true
      }
    },
    // 获取优惠券列表
    async queryCouponList (status) {
      const param = {
        pageNo: this.pageData.pageNo,
        pageSize: this.pageData.pageSize,
        status: status === 1 ? '00' : status === 2 ? '20' : status === 3 ? '30' : '', // 00未使用 10已冻结 20已使用 30已过期
        'ACCESS-TOKEN': this.accessToken
      }
      const res = await this.$api.queryCouponList(param)
      this.pageFlag = false
      this.pageLoad = true
      if (res && res.code == '200') {
        this.allCount = res.data.count
        this.couponList = [...this.couponList, ...res.data.list]
      }
    },
    // 列表切换
    listChange(e) {
      this.currentActive = e.detail.index
      wx.pageScrollTo({
          duration: 0,
          scrollTop: 0,
        });
    },
    // 优惠券状态改变
    couponChange() {
      // this.checkIndex = e.detail.index
      this.resetInfo()
      this.queryCouponList(this.checkIndex)
    },
    // 重置
    resetInfo () {
      this.pageData.pageNo = 1
      this.pageFlag = false
      this.pageLoad = false
      this.couponList = []
    }
  }
};
</script>

<style scoped lang="scss">
.coupon-wrap {
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
  background: #f2f2f2;
}
.my-vip {
  min-height: 100vh;
  overflow: hidden;
  padding-top: 50px;
  background: #f2f2f2;
	.tip{
		width: 100%;
		height: 66rpx;
		line-height: 66rpx;
		background: #EEF5FE;
		color: #999999;
		font-size: 26rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
	}
  .vip-item {
    height: 274rpx;
    width: 738rpx;
    padding: 20rpx 28rpx;
    margin: 0 auto 8rpx;
    background: url($public-baseImgUrl+'mine/vipcard-bg.png') center no-repeat;
    background-size: 100% 100%;
    .top {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      .icon {
        margin-right: 20rpx;
        border: 10rpx solid rgba(255, 255, 255, 0.15);
        border-radius: 50%;
        overflow: hidden;
        // background-color: #F2F2F2;
        image {
          width: 100rpx;
          height: 100rpx;
          vertical-align: middle;
        }
      }
      .title {
				width: 72%;
        color: #ffffff;
        line-height: 40rpx;
      }
    }
    .bottom {
      justify-content:space-between;
      margin-top: 30rpx;
			align-items: center;
			.bottom_left{
				width: 140rpx;
			}
			.bottom_right{
				flex: 1;
				display: flex;
				justify-content: space-between;
				align-items: center;
				.bottom_right_cost{
					display: flex;
					// vertical-align:bottom;
					align-items: flex-end;
					.label {
					  font-size: 24rpx;
						margin-bottom: 6rpx;
					  color: #96c2fc;
					}
					.price {
					  color: #ffffff;
					  font-size: 24rpx;
					  text {
					    font-size: 42rpx;
					  }
					}
				}
				.topUp{
					width: 165rpx;
					height: 72rpx;
					font-size: 30rpx;
					color: #FEFEFE;
					line-height: 72rpx;
					background: #226ED4;
					border-radius: 36rpx;
					text-align: center;
				}
			}
      
    }
  }
  .guide-box {
    justify-content: center;
    .guide-btn {
      height: 78rpx;
      line-height: 74rpx;
      border: 2rpx solid #2e87ff;
      border-radius: 39rpx;
      color: #2e87ff;
      padding: 0 45rpx;
      display: inline-block;
      margin: 20rpx auto;
    }
  }
}
.fixed-line {
  position:fixed;
  top: 50px;
  background:#fff;
  z-index: 10;
  width: 100%;
  height: 50px;
  display:flex;
  border-top: 1px solid #f2f2f2;
  .fixed-nav {
    flex:1;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 26rpx;
    color: #333;
    &.active {
      color: $public-color-blue1
    }
  }
}
</style>
<style lang="scss">
page {
  height: 100%;
  background: #f2f2f2;
}
.van-tabs__nav {
  height: 50px;
  line-height: 50px;

  .van-ellipsis {
    line-height: 50px;
    padding: 0 !important;
  }

  .van-tab--active {
    color: $public-color-blue1;
  }
}

.van-tabs__wrap {
  width: 100%;
  height: 50px !important;
  position: fixed;
  top: 0;
}

.van-tabs {
  background: #f2f2f2!important;
  .van-sticky {
    z-index: 2 !important;
  }
}

.van-tab__pane--active {
  border-top: 1rpx solid #e9e9e9 !important;
}
.coupon {
  // padding-top: 50px;
  margin-top: 100px;
  .van-tabs__wrap {
    position:fixed;
    border-top: 1px solid #f2f2f2 !important;
    top: 50px;
    z-index: 3!important;
  }
    .list-loading {
    width: 750rpx;
    display: flex;
    justify-content: center;
    position: relative;
    padding-top: 400rpx;
  }
  .scroll-bottom {
    line-height: 50rpx;
    color: #666;
    text-align: center;
    height: 50rpx;
    text {
      color: #cccccc;
      font-size: 24rpx;
    }
  }
  box-sizing: border-box;
  .van-tabs__line {
    display: none!important;
  }
  .van-tab__pane--active {
    border:none!important;
  }
}
.van-tabs__scroll--line {
  height: 50px!important;
}
.van-tab__pane--active {
    border: none!important;
  }
</style>
